package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.AdminCommentBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.*;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.*;
import com.cloudbroker.bcs.platform.ltb.middle.entity.*;
import com.cloudbroker.bcs.platform.ltb.middle.service.BlackuserService;
import com.cloudbroker.bcs.platform.ltb.middle.service.CommentService;
import com.cloudbroker.bcs.platform.ltb.middle.service.UserService;
import com.cloudbroker.bcs.platform.ltb.middle.service.BlackWordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes.ClientErr.*;

/**
 * Created by lili19289 on 2016/7/20.
 */
@Service
public class AdminCommentServiceImpl implements AdminCommentBizService {

    @Resource
    private CommentService commentService;

    @Resource
    private BlackWordService blackWordService;

    @Resource
    private BlackuserService blackUserService;

    @Resource
    private UserService userService;
    @Override
    public HSBCPSBackVO funTopComment(CommentIdForm commentIdForm) {
        if(commentService.get(commentIdForm.getComment_id())==null){
            return VOUtil.genErrorResultVo(COMMENT_NOT_EXIT);
        }
        CommentMsg commentMsg = new CommentMsg();
        commentMsg.setComment_id(Long.valueOf(commentIdForm.getComment_id()));
        commentService.topComment(commentMsg);
        return VOUtil.genSuccessBackVo();
    }

    @Override
    public HSBCPSBackVO funCloseComment(CommentIdForm commentIdForm) {
        if(commentService.get(commentIdForm.getComment_id())==null){
            return VOUtil.genErrorResultVo(COMMENT_NOT_EXIT);
        }
        CommentMsg commentMsg = new CommentMsg();
        commentMsg.setComment_id(Long.valueOf(commentIdForm.getComment_id()));
        commentMsg.setClose_status("2");
        commentService.updateComment(commentMsg);
        return VOUtil.genSuccessBackVo();
    }

    public CommentVOResult queryComments(CommentQueryForm commentQueryForm){
        List<CommentUser> commentUsers=commentService.queryComments(commentQueryForm);
        CommentVOResult result = new CommentVOResult();
        result.setData_str( JSON.toJSONString(commentUsers));
        return VOUtil.setSuccessResult(result);
    }

    public BlackWordBack funAddBlackWord(BlackWordForm wordForm){
        BlackWord word = new BlackWord();
        word.setBlackword_content(wordForm.getWord_content());
        word.setCompany_id(wordForm.getCompany_id());
        if(blackWordService.getByContent(word)!=null){
            return VOUtil.genErrorResult(BLACK_WORD_EXIT,BlackWordBack.class);
        }
        blackWordService.save(word);
        BlackWordBack wordBack = JSON.parseObject(JSON.toJSONString(word),BlackWordBack.class);
        BlackWordZK.changeBlackWord(word.getCompany_id());
        return VOUtil.setSuccessResult(wordBack);
    }

    public HSBCPSBackVO funDelBlackWord(BlackWordIdForm wordForm){
        BlackWord word = new BlackWord();
        word.setBlackword_id(Long.parseLong(wordForm.getWord_id()));
        word.setCompany_id(wordForm.getCompany_id());
        blackWordService.deleteBlackWord(word);
        BlackWordZK.changeBlackWord(word.getCompany_id());
        return VOUtil.genSuccessBackVo();
    }

    public HSBCPSBackVO funEditBlackWord(BlackWordContentForm wordForm){
        BlackWord word = new BlackWord();
        word.setBlackword_id(Long.parseLong(wordForm.getWord_id()));
        word.setBlackword_content(wordForm.getWord_content());
        word.setCompany_id(wordForm.getCompany_id());
        if(blackWordService.getByContent(word)!=null){
            return VOUtil.genErrorResult(BLACK_WORD_EXIT,HSBCPSBackVO.class);
        }
        blackWordService.editBlackWord(word);
        BlackWordZK.changeBlackWord(word.getCompany_id());
        return VOUtil.genSuccessBackVo();
    }

    public BlackWordResult funQueryBlackWords(BlackKeyWordForm wordForm){
        List<BlackWord> words = blackWordService.queryBlackWords(wordForm);
        BlackWordResult vo = new BlackWordResult();
        vo.setData_str(JSON.toJSONString(words));
        return VOUtil.setSuccessResult(vo);
    }

    public BlackUserBack funAddBlackUser(UserIdForm userForm){
        BlackUser user = new BlackUser();
        user.setUser_id(userForm.getUser().getUser_id());
        user.setCompany_id(userForm.getCompany_id());
        if(userService.get(userForm.getUser().getUser_id()+"")==null){
            return VOUtil.genErrorResult(USER_NOT_EXIT,BlackUserBack.class);
        }
        if(blackUserService.getBlackUser(user)!=null){
            return VOUtil.genErrorResult(BLACK_USER_EXIT,BlackUserBack.class);
        }
        blackUserService.save(user);
        BlackUserBack userVO = JSON.parseObject(JSON.toJSONString(user),BlackUserBack.class);
        return VOUtil.setSuccessResult(userVO);
    }

    public HSBCPSBackVO funDelBlackUser(BlackUserIDForm userForm){
        BlackUser user = new BlackUser();
        user.setBlackuser_id(Long.parseLong(userForm.getBlackuser_id()));
        user.setCompany_id(userForm.getCompany_id());
        if(blackUserService.getBlackUser(user)==null){
            return VOUtil.genErrorResult(BLACK_USER_NOT_EXIT,BlackUserBack.class);
        }
        blackUserService.deletBlackUser(user);
        return VOUtil.genSuccessBackVo();
    }

    public BlackUserResult funQueryBlackUser(BlackUserForm userForm){
        userForm.getUser();
        BlackUserResult result = new BlackUserResult();
        List<BlackUser> blackUsers = blackUserService.funQueryBlackUser(userForm);
        result.setData_str(JSON.toJSONString(blackUsers));
        return VOUtil.setSuccessResult(result);
    }

    public UserResult funQueryUsers(UserQueryForm form){
        List<UserEntity> users =userService.queryByName(form);
        UserResult result = new UserResult();
        result.setData_str(JSON.toJSONString(users));
        return VOUtil.setSuccessResult(result);
    }
}
